home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_5.lha / 5_5 / 5_5a_all.c next >
C/C++ Source or Header  |  1993-08-08  |  2KB  |  109 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. include <stream.h>
  6. include <string.h>
  7. include <ctype.h>
  8. include <debug.h>
  9. nt error(const char *fmt ...);
  10.  
  11. har prefix[512] = "....", *endprefix = &prefix[1];
  12. har *incr()
  13.  
  14.    *endprefix++ = '.';
  15.    *endprefix = '\0';
  16.    return prefix+1;
  17.  
  18. har *decr()
  19.  
  20.    *--endprefix = '\0';
  21.    return prefix;
  22.  
  23.  
  24. har *unctrl(unsigned int c)
  25.  
  26.    static char x[5];
  27.    char *p = x;
  28.  
  29.    if (c > 0177)
  30. *p++ = 'M', *p++ = '-', c -= 0200;
  31.    if ((c < 040) || (c == 0177))
  32. *p++ = '^', *p++ = c ^ 0100;
  33.    else
  34. *p++ = c;
  35.    *p = '\0';
  36.    return x;
  37.  
  38.  
  39. include "5_5A.h"    /* enum exprtype */
  40. include "5_5B.h"    /* class tree, expr, token */
  41.  
  42. include "5_5a0.c"    /* forward declarations */
  43.  
  44. include "5_5a5.c"    /* prim() */
  45. include "5_5a4.c"    /* term() */
  46. include "5_5a3.c"    /* get_expr() */
  47. include "5_5a2.c"    /* gettoken() */
  48. include "5_5a1.c"    /* expandtree, expr::expr() */
  49.  
  50. include "5_5b2.c"    /* treeval() */
  51. include "5_5b1.c"    /* expr::eval() */
  52.  
  53. include "main.c"
  54.  
  55. / print the expression tree
  56. tatic char *spaces(int num)
  57.  
  58.    return form("%.*s", num * 3, " ");
  59.  
  60.  
  61. oid tree::print(int level)
  62.  
  63.    if (this)
  64. switch (this->type)
  65.     {
  66.     case PLUS:
  67.     case DIV:
  68.     case MUL:
  69.     cout << spaces(level) << chr(this->type) << "\n";
  70.     this->left->print(level + 1);
  71.     this->right->print(level + 1);
  72.     return;
  73.  
  74.     case MINUS:
  75.     cout << spaces(level) << chr(this->type) << "\n";
  76.     if (this->right)
  77.         {
  78.         this->left->print(level + 1);
  79.         this->right->print(level + 1);
  80.         }
  81.     else
  82.         this->left->print(level + 1);
  83.     return;
  84.  
  85.     case NUMBER:
  86.     cout << spaces(level) << "# " << this->value << "\n";
  87.     return;
  88.  
  89.     case LP:
  90.     cout << spaces(level) << chr(this->type) << "\n";
  91.     this->left->print(level + 1);
  92.     return;
  93.  
  94.     case RP:
  95.     case END:
  96.     default:
  97.     cout << spaces(level) << chr(this->type) << "\n";
  98.     error("invalid type within tree");
  99.     break;
  100.     }
  101.  
  102.    else
  103. error("NULL node found");
  104.    return;
  105.  
  106.  
  107. oid expr::eprint()
  108.  if (debug) head->print(0); }
  109.